using System;
using Microsoft.SPOT;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.Netduino;
using System.Threading;

namespace Netduino4Fun.DesktopAssistant.Netduino
{
    class RgbLed
    {
        const int TransitionSteps = 50;
        const int TransitionStepDuration = 20;

        PWM redPin = new PWM(Pins.GPIO_PIN_D9);
        PWM greenPin = new PWM(Pins.GPIO_PIN_D10);
        PWM bluePin = new PWM(Pins.GPIO_PIN_D6);

        public int Red { get; private set; }
        public int Green { get; private set; }
        public int Blue { get; private set; }


        public void ChangeColor(int red, int green, int blue)
        {
            greenPin.SetDutyCycle((uint)green);
            redPin.SetDutyCycle((uint)red);
            bluePin.SetDutyCycle((uint)blue);
           //ColorTransition(red, green, blue);
        }

        private void ColorTransition(int newRed, int newGreen, int newBlue)
        {
            float deltaGreen = ((float)(newGreen - Green)) / TransitionSteps;
            float deltaRed = ((float)(newRed - Red)) / TransitionSteps;
            float deltaBlue = ((float)(newBlue - Blue)) / TransitionSteps;

            for (int i = 1; i <= TransitionSteps; i++)
            {
                uint transitionGreen = (uint)(Green + i * deltaGreen);
                uint transitionRed = (uint)(Red + i * deltaRed);
                uint transitionBlue = (uint)(Blue + i * deltaBlue);

                greenPin.SetDutyCycle(transitionGreen);
                redPin.SetDutyCycle(transitionRed);
                bluePin.SetDutyCycle(transitionBlue);

                Thread.Sleep(TransitionStepDuration);
            }

            Green = newGreen;
            Red = newRed;
            Blue = newBlue;
        }
    }
}
